<%@page import="weaver.conn.RecordSet"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="com.zlgs.cqyf.vote.ConnectionUtil"%>
<%@ page import="com.alibaba.fastjson.*" %>
<%@ page import="java.io.PrintWriter" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="weaver.hrm.User" %>
<%@ page import="weaver.hrm.HrmUserVarify" %>
<%
    response.setCharacterEncoding("UTF-8");
    response.setContentType("application/json; charset=utf-8");
    JSONObject retObject = new JSONObject();
   
    try {
	        JSONObject jsonObject = new JSONObject();
	   	 	retObject.put("code", 200);
	   	    retObject.put("msg", "操作成功");
  	    // 获取用户信息
  	  		String userId ="";
  	        String deptId = "";
  	        String sql = "";
  	        String name = "";
  	        String dept = "";
   
  	        Object requestType = request.getParameter("requestType");
  			if(null != requestType && !"".equals(requestType.toString())&&"mobile".equals(requestType.toString())){
  				Object userIdObj = request.getParameter("userId");
  				if(null == userIdObj || "".equals(userIdObj.toString())){
  					throw  new RuntimeException("请求用户Id为空!");
  				}
  				 userId = userIdObj.toString();
  				 String userSQL = "select top 1 * from HrmResource where   loginid = N'"+userId+"' ORDER BY ID DESC";
  				 RecordSet  userSet= new RecordSet();
  				 userSet.executeSql(userSQL);
  				 if(userSet.next()){
  					 name =  userSet.getString("lastname");
  					 deptId = userSet.getString("departmentid");
  				 }else{
  					 throw  new RuntimeException("该用户不存在于OA!");
  				 }
  				 sql = "select * from HrmDepartment where id='"+deptId+"' ";
  			        RecordSet  set0 = new RecordSet();
  			        set0.executeSql(sql);
  			        if (set0.next()){
  			        	retObject.put("deptId",deptId);
  			            dept = set0.getString("departmentname");
  			        }
  			}else{
  				if (!ConnectionUtil.is_local){
  				    User user = HrmUserVarify.getUser (request , response) ;
  		            if (null == user)  throw new RuntimeException("请重新登录");
  		            userId = user.getLoginid();
  		            name = user.getUsername();
  		            // 留言人部门名称
  		            deptId = String.valueOf(user.getUserDepartment());
  		        	retObject.put("deptId",deptId);
  		            sql = "select * from HrmDepartment where id="+deptId;
  		            RecordSet  set = new RecordSet();
  		            set.executeSql(sql);
  		            while (set.next()){
  		                dept = set.getString("departmentname");
  		            }
  				}
  				
  			 }
    	
    	
        // URL 参数
        Integer awardsParticipatorId = Integer.parseInt(request.getParameter("awardsParticipatorId"));
        if (null != awardsParticipatorId){
            // 返回参数（是否投过票/是否送过花）
            jsonObject.put("votesStatus",false);
            jsonObject.put("flowerStatus",false);
            if(awardsParticipatorId != null){
                // 数据库查询
                RecordSet set1  = new RecordSet();	
                sql = "SELECT * from vote_awards_flower where awards_participator_id="+awardsParticipatorId.intValue()+" and user_id= N'"+userId+"'";
                set1.executeQuery(sql);
                while (set1.next()){
                    jsonObject.put("flowerStatus",true);
                    break;
                }
                sql = "SELECT * from vote_awards_ballot where awards_participator_id="+awardsParticipatorId.intValue()+" and  user_id= N'"+userId+"'";
                RecordSet set  = new RecordSet();	
                while (set.next()){
                    jsonObject.put("votesStatus",true);
                    break;
                }
            }
        }
        retObject.put("data", jsonObject);
    } catch (Exception e) {
        e.printStackTrace();
        retObject.put("code", -1);
        retObject.put("msg", e.getMessage());
    } finally {
        PrintWriter printWriter = response.getWriter();
        printWriter.append(retObject.toJSONString());
        if (printWriter != null)  printWriter.close();
    }
%>
